﻿namespace SharpF2.ModelView
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Diagnostics;
	using System.IO;
	using System.Windows;
	using System.Windows.Forms;
	using Components;
	using Properties;
	using Services;
	using MessageBox=System.Windows.MessageBox;

	public partial class MainViewViewModel
	{
		#region Private Members

		private static IEnumerable<String> GetFilesFromFileDialog()
		{
			var initialDirectory = GetLastOpenFilePath();

			var dialog = new OpenFileDialog
			             	{
			             		InitialDirectory = initialDirectory,
			             		CheckFileExists = true,
			             		FileName = String.Empty,
			             		Multiselect = true,
			             		Filter = "All files|*.*"
			             	};

			if (DialogResult.OK != dialog.ShowDialog())
				return new String[0];

			var filePaths = dialog.FileNames;

			if (filePaths.Length > 0)
				SaveOpenFilePath(Path.GetDirectoryName(filePaths[0]));

			return filePaths;
		}

		private static IEnumerable<String> GetFilesFromFolderDialog()
		{
			var path = GetLastOpenFolderPath();

			var dialog = new FolderBrowserDialog
			             	{
			             		SelectedPath = path,
			             		ShowNewFolderButton = true,
			             	};

			if (DialogResult.OK != dialog.ShowDialog() || !Directory.Exists(dialog.SelectedPath))
				return new String[0];

			SaveOpenFolderPath(dialog.SelectedPath);

			return Directory.GetFiles(dialog.SelectedPath);
		}

		private static String GetLastOpenFilePath()
		{
			var path = Settings.Default.OpenFileLastPath;
			return !String.IsNullOrEmpty(path) ? path : Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
		}

		private static String GetLastOpenFolderPath()
		{
			var path = Settings.Default.OpenFolderLastPath;
			return !String.IsNullOrEmpty(path) ? path : Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
		}

		private static void OpenUpdateExplorer()
		{
			if (!WebServices.Instance.NewVersionAvailable) return;

			var result = MessageBox.Show(
				String.Format(Resources.NewVersionAvailableDialogInfo,
				              WebServices.Instance.OnlineVersion ?? String.Empty),
				Resources.NewVersionAvailableDialogTitle,
				MessageBoxButton.YesNo,
				MessageBoxImage.Information,
				MessageBoxResult.Yes);

			if (MessageBoxResult.No.Equals(result)) return;

			Process.Start("IEXPLORE.EXE", WebServices.Instance.UpdateURI);
		}

		private static void SaveOpenFilePath(String path)
		{
			Settings.Default.OpenFileLastPath = path;
			Settings.Default.Save();
		}

		private static void SaveOpenFolderPath(String path)
		{
			Settings.Default.OpenFolderLastPath = path;
			Settings.Default.Save();
		}

		private static void UpdateSortDescription<Item>(GridViewColumnExtension<Item> column,
		                                                ref SortDescription sortDescription)
		{
			if (!String.IsNullOrEmpty(column.PropertyName) &&
			    column.PropertyName.Equals(sortDescription.PropertyName))
				sortDescription.Direction = sortDescription.InvertDirection();
			else
			{
				sortDescription.Direction = ListSortDirection.Ascending;
				sortDescription.PropertyName = column.PropertyName;
			}
		}

		#endregion
	}
}